<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include 'GeneralController.php';
class HomeController extends GeneralController {

        /*
         * Index page
         */
	
        public function index()
	{
            //$voyages = $this->Voyage->getVoyages($this->pagination->per_page,1);
            $this->firstLoads();
            $nbRes=$this->LigneVoyage->count($this->formFilter);
            $this->configure($nbRes);
            $lignesVoyages = $this->LigneVoyage->getLignesVoyages($this->pagination->per_page,1,$this->formFilter);
            
            $this->loadLayoutView('home',array('lignesVoyages'=>$lignesVoyages, "formFilter"=>$this->formFilter,"nbRes"=>$nbRes));
	}
        
        public function search($page)
        {
            $this->firstLoads();
            $nbRes=$this->LigneVoyage->count($this->formFilter);
            $this->configure($nbRes);
            $lignesVoyages = $this->LigneVoyage->getLignesVoyages($this->pagination->per_page,$page,$this->formFilter);

            $this->loadLayoutView('home',array("name"=>"Bernard",'lignesVoyages'=>$lignesVoyages,'baseUrl'=>base_url(),'pagination'=>$this->pagination, "formFilter"=>$this->formFilter,"nbRes"=>$nbRes));
        }
        
        public function ajaxCommande($page)
        {
            $this->configure();
            $lignesVoyages = $this->LigneVoyage->getLignesVoyages($this->pagination->per_page,$page,$this->formFilter);

            $this->loadLayoutView('home',array("name"=>"Bernard",'lignesVoyages'=>$lignesVoyages,'baseUrl'=>base_url(),'pagination'=>$this->pagination));
        }
        
        public function ajaxGetLigneVoyage(){
            $this->load->database();
            $this->load->model('Voyage');
            $this->load->model('LigneVoyage');
            $this->load->helper('url');
            $lv= $this->LigneVoyage->getLigneVoyage($_POST["id_ligne_voyage"]);
            $v= $lv->getVoyage();
            $v->photo =base_url().$v->getPhoto();
            $lv->voyage = $v;
            echo json_encode($lv);
        }

        private function firstLoads(){
            $this->load->database();
            $this->load->model('Voyage');
            $this->load->model('LigneVoyage');
            $this->load->model('Pays');
            $this->load->model('Ville');
            $this->load->model('TypeVoyage');
            $this->load->library('pagination');
            $this->load->helper('url');
            $this->formFilter = array("ListePays"=>$this->Pays->getAll());
            $this->formFilter["ListeVilles"]=$this->Ville->getAll();
            $this->formFilter["ListeTypes"]= $this->TypeVoyage->getAll();
            $this->formFilter["ListeMois"]= array("01"=>"Janvier",
                "02"=>"Février",
                "03"=>"Mars",
                "04"=>"Avril",
                "05"=>"Mai",
                "06"=>"Juin",
                "07"=>"Juillet",
                "08"=>"Aôut",
                "09"=>"Septembre",
                "10"=>"Octobre",
                "11"=>"Novembre",
                "12"=>"Décembre");
            $this->formFilter["ListeDuree"]= array("1"=>"2-3 jours",
                "2"=>"4-10 jours",
                "3"=>"10-15 jours",
                "4"=>"+15 jours");
            $this->formFilter["ListeAnnee"]= array("2015","2016");
            $this->updateSearchParams();
        }
        private function configure($total_rows = 10){
            $config['base_url'] = base_url().'search/page/';
            $config['total_rows'] = $total_rows;
            $config['per_page'] = 5; 
            $config['use_page_numbers'] = FALSE;
            $config['num_links'] = 2;
            $this->pagination->initialize($config); 

        }
        
        private function updateSearchParams(){
            // Sauvegarde en session les parametres de recherhe 
            // pour grader les souvenirs des critères de recherches pour les différentes pages
            $params=array("pays","ville","annee","mois","duree","type");
            $this->load->library('session');
            foreach ($params as $param) {
                if(isset($_POST[$param])){
                    $this->session->set_userdata($param,$_POST[$param]);
                    $this->formFilter[$param]= $_POST[$param];                
                }else{
                    $this->formFilter[$param]= $this->session->userdata($param);
                }

            }
        }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */